package com.zhangtai.modules.service.impl.leave;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.ShiftDao;
import com.zhangtai.modules.entity.leave.ShiftEntity;
import com.zhangtai.modules.service.leave.ShiftService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class ShiftServiceImpl extends ServiceImpl<ShiftDao, ShiftEntity> implements ShiftService {

    @Override
    public PageResult queryPage(@RequestParam(value = "shiftName", required = false) String shiftName,
                                @RequestParam(value = "shiftOid", required = false) String shiftOid,
                                @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                @RequestParam(value = "shiftOrgName", required = false) String shiftOrgName,
                                @RequestParam(value = "shiftUnitName", required = false) String shiftUnitName,
                                @RequestParam(value = "shiftJobName", required = false) String shiftJobName,
                                @RequestParam(value = "shiftDateStart", required = false) String shiftDateStart,
                                @RequestParam(value = "shiftDateEnd", required = false) String shiftDateEnd,
                                @RequestParam(value = "frequency", required = false) List<Integer> frequency,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ShiftEntity> page = this.page(
                new Page<ShiftEntity>(pageNum,pageSize),
                new QueryWrapper<ShiftEntity>()
                        .like(!StringUtils.isEmpty(shiftName),"shift_name",shiftName)
                        .eq(!StringUtils.isEmpty(shiftOid),"shift_oid",shiftOid)
                        .like(!StringUtils.isEmpty(shiftOrgName),"shift_org_name",shiftOrgName)
                        .like(!StringUtils.isEmpty(shiftUnitName),"shift_unit_name",shiftUnitName)
                        .like(!StringUtils.isEmpty(shiftJobName),"shift_job_name",shiftJobName)
                        .ge(!com.zhangtai.utils.StringUtils.isEmpty(shiftDateStart),"shift_date",shiftDateStart+" 00:00:00")
                        .le(!com.zhangtai.utils.StringUtils.isEmpty(shiftDateEnd),"shift_date",shiftDateEnd+" 29:59:59")
                        .in(!CollectionUtils.isEmpty(frequency),"frequency",frequency)
                        .and(ObjectUtils.isEmpty(shiftOid), war->war.in(!CollectionUtils.isEmpty(orgIds),"shift_unit_id",orgIds).
                        or().eq(CollectionUtils.isEmpty(orgIds),"shift_unit_id",null))
                        .eq("is_delete",0)
                        .orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public ShiftEntity getByIdE(Long id) {
            ShiftEntity one = this.getOne(new QueryWrapper<ShiftEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(ShiftEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(ShiftEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            ShiftEntity entity = new ShiftEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}